package com.kkb.dao.impl;

import com.kkb.bean.User;
import com.kkb.dao.BaseAdminDao;
import com.kkb.utils.DateFormatUtil;
import com.kkb.utils.DruidUtil;

import javax.persistence.Id;
import javax.persistence.criteria.CriteriaBuilder;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class AdminDaoMysql implements BaseAdminDao {

    private static final String SQL_UPDATE_LOGIN_TIME = "UPDATE EADMIN SET LOGINTIME = ? ,LOGINIP = ? WHERE USERNAME =?";
    private static final String SQL_LOGIN = "SELECT * FROM EADMIN WHERE USERNAME=? AND PASSWORD =?";
    private static final String SQL_UPDATE = "UPDATE EADMIN SET USERNAME=?,PASSWORD=?,IDCARD=?,PHONENUMBER=? WHERE ID=?";
    private static final String SQL_FIND_BY_PHONE = "SELECT * FROM EADMIN WHERE PHONENUMBER=?";
    private static final String SQL_INSERT = "INSERT INTO EADMIN(USERNAME,PASSWORD,IDCARD,PHONENUMBER,CREATETIME) VALUES(?,?,?,?,NOW())";
    private static final String SQL_DELETE = "DELETE FROM EADMIN WHERE ID = ? ";
    private static final String SQL_FIND_ALL = "SELECT * FROM EADMIN";
    private static final String SQL_FIND_BY_LIMIT = "SELECT * FROM EADMIN LIMIT ?,?";
    private static final String SQL_CONSOLE = "SELECT COUNT(ID) DATA1 , COUNT(TO_DAYS(CREATETIME) = TO_DAYS(NOW()) OR NULL)  DATA2 FROM EADMIN ";

    /**
     * 根据用户名 更新登录时间和规范
     *
     * @param username
     * @param date
     * @param ip
     */
    @Override
    public void updateLoginTime(String username, Date date, String ip) {
        // 1. 获取连接
        Connection connection = DruidUtil.getConnection();
        PreparedStatement preparedStatement = null;
        try {
            // 2. 预编译sql语句
            preparedStatement = connection.prepareStatement(SQL_UPDATE_LOGIN_TIME);
            // 3. 填充参数
            preparedStatement.setString(1, DateFormatUtil.format(date));
            preparedStatement.setString(2,ip);
            preparedStatement.setString(3,username);
            // 4. 执行
            preparedStatement.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 5. 释放资源
            DruidUtil.close(connection,preparedStatement,null);
        }
    }

    /**
     * 管理员根据账号密码登录
     *
     * @param username 账号
     * @param password 密码
     * @return 用户信息
     */
    @Override
    public boolean login(String username, String password) {
        // 1. 获取连接
        Connection connection = DruidUtil.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;
        try {
            // 2. 预编译sql语句
            preparedStatement = connection.prepareStatement(SQL_LOGIN);
            // 3. 填充参数
            preparedStatement.setString(1,username);
            preparedStatement.setString(2,password);
            // 4. 执行
            rs = preparedStatement.executeQuery();
            // 5. 查询结果
            return  rs.next();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 5. 释放资源
            DruidUtil.close(connection,preparedStatement,rs);
        }
        return false;
    }

}
